package com.lnsoft.cac.server.handle.webservice.MsgHandle;

import org.jdom2.Document;
import org.jdom2.Element;

import lombok.extern.slf4j.Slf4j;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

@Slf4j
public class UploadCACConfig {
    private Element rootNode;
    private Document doc;
    private Element configsNode;
    private Element configNode;

    public UploadCACConfig() {
        this.rootNode = new Element("request");
        this.doc = new Document(this.rootNode);
        this.configsNode = new Element("configs");
        this.rootNode.addContent(this.configsNode);
        this.configNode = new Element("config");
        this.configsNode.addContent(this.configNode);
    }

    public void setConfigNodeAttr(String strDeviceId, String strMeasuerPointId, String strCommandId) {
        this.configNode.setAttribute("objid", strDeviceId);
        this.configNode.setAttribute("measurepointno", strMeasuerPointId);
        this.configNode.setAttribute("commandid", strCommandId);
    }

    public void addAttrNode(String strName, String strValue) {
        Element attrElement = new Element("attr");
        attrElement.setAttribute("desc", strName);
        attrElement.setAttribute("name", strName);
        attrElement.setAttribute("value", strValue);

        //添加到父节点
        this.configNode.addContent(attrElement);
    }

    public String getMsg() {
        XMLOutputter XMLOut = new XMLOutputter(Format.getPrettyFormat());
        return XMLOut.outputString(this.doc);
    }
}
